package com.red.wood.module.cdn.mybatis.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.red.wood.common.domain.Page;
import com.red.wood.common.springjdbc.SqlServerDao;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;
import com.red.wood.module.cdn.mybatis.dao.CdnHouseInfoDao;

@Service
public class CdnHouseInfoService {

	@Autowired
	private CdnHouseInfoDao dao;
	
	@Autowired
	private SqlServerDao sqlServerDao;
	
	public Map<String, Object> getPageData(IMap param) {
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		IMap map = sqlServerDao.getUserLevelInfo(param.getString("userId"));
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(map.get("loginUser")));
		String provinceId = CommonUtils.getProvinceId(userProvince);
		int pageSize = page.getPageSize();
		String enterprise = param.getString("enterprise");
		param.put("enterprise", enterprise);
		param.put("userLevel", map.get("userLevel"));
		param.put("userProvince", userProvince);
		param.put("provinceId", provinceId);
		param.put("pageSize", pageSize);
		param.put("offset", offset);
		
		long count = 0;
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		//如果勾选企业、企业+机房，并且选择了外省，那么返回空
		if((param.getString("enterprise").equals("2")  && param.getString("userLevel") .equals("4"))||(param.getString("enterprise").equals("2")  && param.getString("userLevel") .equals("1"))){
			count= 0;
	    }
	    else{
	        count = dao.getCount(param);
			list = filter(dao.getPageData(param));
		}
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	
	public List<Map<String, Object>> filter(List<Map<String, Object>> list){
		if(list != null && list.size() > 0) {
			for(Map<String,Object> map : list) {
				map.put("ipNum",CommonUtils.filteNull(map.get("ipNum")));
				map.put("bandWidth",CommonUtils.filteNull(map.get("bandWidth")));
			}
		}
		return list;
	}
}
